package com.nianzhen.recruitment;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nianzhen.utils.App;

public abstract class BaseFragment extends Fragment{

	protected View mView;

	private ImageView mBackBtn;
	private TextView mTitleTv;
	private TextView mRightTv;

	protected Context mCtx = App.getContext();
	

	protected void inflate(LayoutInflater inflater,int resId,ViewGroup container){
		mView = inflater.inflate(resId, container, false);
		initTitle();
	}

	protected void initTitle(){
		if (mView != null){
			if (mBackBtn == null){
				mBackBtn = (ImageView) mView.findViewById(R.id.header_back_btn);
				if(mBackBtn != null){
					mBackBtn.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View arg0) {
							if(onInterceptBack()){
								return;
							}else{
								onLeftPressed();
							}
						}
					});
				}
			}

			if (mRightTv == null){
				mRightTv = (TextView) mView.findViewById(R.id.header_right_tv);
				if (mRightTv != null){
					mRightTv.setOnClickListener(new View.OnClickListener(){
						@Override
						public void onClick(View v) {
							onRightPressed();
						}
					});
				}
			}
			if (mTitleTv == null){
				mTitleTv = (TextView) mView.findViewById(R.id.header_tv);
			}
		}
	}

	public boolean onInterceptBack(){
		return false;
	}

	public void onRightPressed(){

	}

	public void onLeftPressed(){
		Activity activity = getActivity();
		if (activity != null){
			activity.finish();
		}
	}

	public void setTitle(int resId){
		if(mTitleTv != null){
			mTitleTv.setText(resId);
		}
	}

	public void setTitle(String str){
		if(mTitleTv != null){
			mTitleTv.setText(str);
		}
	}

	public void setHeaderRightText(String str){
		if (mRightTv!=null){
			mRightTv.setText(str);
		}
	}

	public void showBackBtn(){
		if(mBackBtn != null){
			mBackBtn.setVisibility(View.VISIBLE);
		}
	}

	public void hideBackBtn(){
		if(mBackBtn != null){
			mBackBtn.setVisibility(View.GONE);
		}
	}

	public void setHeaderRightText(int resId){
		if(mRightTv != null){
			mRightTv.setText(resId);
		}
	}

}
